/**
 * JAVACC DEMO 1.0
 */
package com.apache.uct.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.uct.common.entity.Role;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * description:  获取角色列表插件
 *
 * @author iussoft 创建时间：2016-3-26
 */
public class RoleListPlugin implements PluginConnector {

    private Logger log = LoggerFactory.getLogger(RoleListPlugin.class);

    private IDao roleDao;

    /**
     * TODO 获取角色列表插件(需要传当前用户的所属机构ID).  <br>
     * 参数说明:<br>
     * userEname:用户账号;查询指定用户的角色列表<br>
     * role属性:按角色查询角色列表;orgId:当前用户机构ID<br>
     * 分页设置:<br>
     * pageSize:每页记录数;<br>
     * pageIndex:当前页码;
     */
    public Object execute(ParamsVo vo) throws Exception {
        ResultEntity result = new ResultEntity();
        try {
            result.setResult("true");
            String userEname = String.valueOf(vo.getParams("userEname"));
            if (Validator.isNull(userEname)) {
                MethodParam param = setMethodParams(vo, 2);
                if (Validator.isEmpty(param)) {
                    result.setResult("false");
                    result.setMessage("查询条件为空或不存在相关信息");
                    return result;
                }
                int pageSize = Integer.valueOf(
                        Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "0"));
                int pageIndex = Integer.valueOf(
                        Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "0"));
                if (pageSize == 0 && pageIndex == 0) {
                    param.setOrderby(" create_time desc");
                    result.setEntity(roleDao.select(param));
                } else {
                    param.setPageIndex(pageIndex);
                    param.setPageSize(pageSize);
                    param.setOrderby(" create_time desc");
                    Page page = roleDao.pageSelect(param);
                    result.setEntity(page);
                }
            } else {
                MethodParam param = new MethodParam("ByUserEname", "", "", Role.class.getName());
                param.setParams("userEname", userEname);
                param.setSqlStr(roleDao.getSql(2)
                        + " and roleId in(select roleId from RoleGive where userEname=:userEname)");
                result.setEntity(roleDao.select(param));
            }
            result.setMessage("查询成功");
        } catch (Exception e) {
            result.setResult("false");
            log.error("执行机构查询失败 ", e);
            result.setMessage("机构查询失败！");
        }
        return result;
    }

    private MethodParam setMethodParams(ParamsVo<Role> vo, int type) {
        StringBuffer sb = new StringBuffer(roleDao.getSql(type));
        MethodParam param = new MethodParam("ByPropertyOrgId", "", "", Role.class.getName());
        String str = buildParams(param, vo);
        if (Validator.isNull(str)) {
            String all = String.valueOf(vo.getParams("roleAll"));//获取所有用户列表
            if (Validator.isNull(all)) {
                return null;
            }
        } else {
            sb.append(str);
        }
        sb.append(" order by createTime desc");
        param.setSqlStr(sb.toString());
        return param;
    }

    private String buildParams(MethodParam param, ParamsVo vo) {
        StringBuffer sb = new StringBuffer();
        if (Validator.isNotNull(String.valueOf(vo.getParams("sysEname")))) {
            sb.append(" and sysEname = :sysEname");
            param.setParams("sysEname", String.valueOf(vo.getParams("sysEname")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("roleEname")))) {
            sb.append(" and roleEname = :roleEname");
            param.setParams("roleEname", String.valueOf(vo.getParams("roleEname")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("roleCname")))) {
            sb.append(" and roleCname = :roleCname");
            param.setParams("roleCname", String.valueOf(vo.getParams("roleCname")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("roleRemark")))) {
            sb.append(" and roleRemark = :roleRemark");
            param.setParams("roleRemark", String.valueOf(vo.getParams("roleRemark")));
        }
        String userOrgId = String.valueOf(vo.getParams("userOrgId"));
        String orgId = String.valueOf(vo.getParams("orgId"));
        if (Validator.isNull(userOrgId)) {
            if ("1".equals(SystemTools.getInstance().getValue("org_act_switch"))) {//是否开启机构授权
                if (Validator.isNotNull(orgId)) {
                    sb.append(
                            " and (delStatus is null or roleId in(select roleId from RoleOrg where orgId='"
                                    + orgId + "') )");
                    param.setParams("orgId", orgId);
                }
            } else {
                sb.append(" and delStatus =:delStatus");
                param.setParams("delStatus", orgId);
            }
        } else {
            orgId = Validator.getDefaultStr(orgId, userOrgId);
            if (Validator.isNotNull(orgId)) {//开启分级授权
                if ("1".equals(SystemTools.getInstance().getValue("org_act_switch"))) {
                    if (!"1".equals(String.valueOf(vo.getParams("sysUser")))) {
                        sb = new StringBuffer();
                        sb.append(roleDao.getSql(3));
                        sb.append(" and orgId = :orgId");
                        param.setParams("orgId", orgId);
                        param.setParams("sysRoles", orgId);
                        param.setKey("ByRoleOrgId");
                    } else {
                        if (Validator.isNull(sb.toString())) {
                            vo.setParams("roleAll", "roleAll");
                        }
                    }
                } else {
                    String delStatus = String.valueOf(vo.getParams("delStatus"));
                    if (Validator.isNotNull(delStatus)) {//存机构ID
                        sb.append(" and delStatus =:delStatus");
                        param.setParams("delStatus", delStatus);
                    }
                    vo.setParams("roleAll", "roleAll");
                }
            } else {
                String delStatus = String.valueOf(vo.getParams("delStatus"));
                if (Validator.isNotNull(delStatus)) {//存机构ID
                    sb.append(" and delStatus =:delStatus");
                    param.setParams("delStatus", delStatus);
                }
                vo.setParams("roleAll", "roleAll");
            }
        }
        return sb.toString();
    }

    public void setRoleDao(IDao roleDao) {
        this.roleDao = roleDao;
    }
}
